<?php
/**
 * @author GhostWei
 * @date Sep 13, 2012
 */
class system_user_controller_category extends system_user_controller_panel {

	protected $pid = null;
	protected $concept = null;
	public function init() {
		if(!parent::init()) {
			return ;
		}
		picture_category::$_lang = $this->user->getLang();
		$this->concept = new picture_category_localeConcept($this->user->getLang());
		$this->view->pid = $this->pid = intval($this->_param('parent'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Pictures'))->setRoute('picture/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Categories'))->setRoute('picture/category/index'));
	}

	protected function _specialList(picture_category_row $row) {
		$lang = $this->user->getLang();
		$adapter = new system_paginator_adapter_concept(new picture_special_concept($lang));
		$adapter->setCurrentPageNumber(intval($this->_param('page', 1)));
		$adapter->where("special.id IN(SELECT `special_id` FROM `picture_special_category` WHERE `lang` = '$lang' AND `category_id` = ?)", $row->getId());
		$keyword = trim($this->_get('keyword'));
		if($keyword == self::$_keyword) $keyword = '';
		if($keyword) {
			$keyword = addslashes($keyword);
			$in = "SELECT `special_id` FROM `picture_special_locale` WHERE `title` LIKE '%$keyword%' AND `lang` = '$lang'";
			$adapter->where("`name` LIKE '%$keyword%' OR `id` IN($in)");
		}
		$this->view->order = $order = strtolower($this->_get('order'));
		$_order = array("display_order ASC", "id DESC");
		if($order) {
			$this->view->sort = $sort = intval($this->_get('sort'));
			switch($order) {
				case 'date': {
					$order = 'lastmodified';
					break;
				}
				case 'photo': {
					$order = 'ptotal';
					break;
				}
				case 'creationtime': case 'id': {
					break;
				}
				default: {
					$sort = !$sort;
					$order = 'display_order';
					break;
				}
			}
			$_sort = !$sort ? 'DESC' : 'ASC';
			$_order = array("$order $_sort");
		}
		$adapter->setLimit(30);
		$adapter->order($_order);
		$list = new system_paginator($adapter);
	
		$sids = $categories = $keywords = array();
		foreach($list AS $row) {
			$sids[$row->getId()] = $row->getId();
		}
		if(!empty($sids)) {
			$pid = implode(', ', $sids);
			$sql = "";
			$sql .= "SELECT a.`special_id`, b.`category_id`, b.`title` FROM `picture_category_locale` AS b ";
			$sql .= "INNER JOIN `picture_special_category` AS a ON a.`category_id` = b.`category_id` ";
			$sql .= "WHERE a.`special_id` IN($pid) AND b.`lang` = '{$this->user->getLang()}'";
			foreach(system_db::getAdapter()->fetchAll($sql) AS $row) {
				$categories[$row['special_id']][] = $row;
			}
			$sql = "";
			$sql .= "SELECT a.`special_id`,a.`keyword_id`,b.`name` FROM `picture_special_keyword` AS a ";
			$sql .= "INNER JOIN `resource_keyword` AS b ON a.`keyword_id` = b.`id` ";
			$sql .= "WHERE a.`special_id` IN($pid) AND b.`lang` = '{$this->user->getLang()}'";
			foreach(system_db::getAdapter()->fetchAll($sql) AS $row) {
				$keywords[$row['special_id']][] = $row;
			}
		}
	
		$this->view->categories = $categories;
		$this->view->keywords = $keywords;
		$this->view->list = $list;
		$this->view->referer = $this->referer;
	}
	
	protected function _getRow() {
		$id = intval($this->_param('id'));
		if(!($row = $this->concept->fetch(array('category.id = ?' => $id)))) {
			throw new picture_exception("The Category is not Existing.");
		}
		return $row;
	}
	
	protected function _saveRow($row) {
		if(!$this->getRequest()->isPost()) {
			return $row;
		}
		$manual = $this->_post('manualKeyNameCheck');
		if(!$row->getId() && !$manual) {
			$name = system_encrypt::createIdByTime();
		} else {
			$name = trim($this->_post('name'));
		}
		$this->view->manualKeyNameCheck = $manual ? true : false;
		$this->referer = $this->_post('referer');
		$row->getLocale()->setTitle(trim($this->_post('title')));
		$row->getLocale()->setDescription($this->_post('description'));
		$row->setName($name);
		$row->getLocale()->setParentId(intval($this->_post('parent_id')));
		$row->getLocale()->setDisplayOrder(intval($this->_post('display_order')));
		$row->getLocale()->setIsActive(intval($this->_post('is_active')) ? 1 : 0);
		$this->_saveToDatabase($row, 'cover', 'image');
		return $row;
	}
	
	protected function _navPage($id = null) {
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('picture/category/list'));
		if(1 && $path = picture_category::categoryPath(($id === null ? $this->pid : $id))) {
			krsort($path);
			foreach($path AS $row) {
				$this->_crumbs()->addPage(system_nav_page::create($row->getTitle())->setRoute('picture/category/list')->setParams(array('parent' => $row->getId())));
			}
		}
	}

}
